<?php

declare(strict_types=1);

namespace app\common\service\crm;

use app\common\traits\ServiceTrait;
use app\common\dao\crm\AchievementDao;
use app\common\dao\sys\UserDao;

/**
 * 业绩目标表 服务业务层(Service)
 * @author 吕玉冰
 * @date  2022-07-19
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class AchievementService
{
    protected $dao = null;

    public function __construct()
    {
        $this->dao = new AchievementDao();
        $this->UserDao = new UserDao();
    }

    use ServiceTrait;

    public function _list_format($data)
    {
        return $data;
    }

    public function _list_where($data)
    {
        $where = [];
        if (isset($data['year']) && !empty($data['year'])) {
            $where[] = ['year', '=', $data['year']];
        } else {
            $where[] = ['year', '=', date("Y")];
        }
        $result['where'] = $where;
        return $result;
    }


    /**
     * lvyubing自动生成数据
     */
    public function buildData($data)
    {


        //获取当前用户或者下级用户
        $field = ['id', 'real_name as user_name'];
        $where = [];
        $where[] = ["id|super_id", "=", $data['user_id']];
        $list = $this->UserDao->getListAll($where, false, $field);
        $save = [];
        foreach ($list as $key => $value) {
            //业绩目标
            $temp = [];
            $temp['year'] = isset($data['year']) ? $data['year'] : date("Y");
            $temp['user_id'] = $value['id'];
            $temp['user_name'] = $value['user_name'];
            $temp['type'] = 3;
            $temp["january"] = 0.0;
            $temp["february"] = 0.0;
            $temp["march"] = 0.0;
            $temp["april"] = 0.0;
            $temp["may"] = 0.0;
            $temp["june"] = 0.0;
            $temp["july"] = 0.0;
            $temp["august"] = 0.0;
            $temp["september"] = 0.0;
            $temp["october"] = 0.0;
            $temp["november"] = 0.0;
            $temp["december"] = 0.0;
            $temp["target_type"] = 1;
            $w = [];
            $w[] = ["year", "=", $temp['year']];
            $w[] = ["user_id", "=", $temp['user_id']];
            $w[] = ["target_type", "=", 1];
            $result = [];
            $result['where'] = $w;
            if ($this->dao->getCount($result) == 0) {
                $this->dao->add($temp);
            }

            //回款目标
            $temp = [];
            $temp['year'] = isset($data['year']) ? $data['year'] : date("Y");
            $temp['user_id'] = $value['id'];
            $temp['user_name'] = $value['user_name'];
            $temp['type'] = 3;
            $temp["january"] = 0.0;
            $temp["february"] = 0.0;
            $temp["march"] = 0.0;
            $temp["april"] = 0.0;
            $temp["may"] = 0.0;
            $temp["june"] = 0.0;
            $temp["july"] = 0.0;
            $temp["august"] = 0.0;
            $temp["september"] = 0.0;
            $temp["october"] = 0.0;
            $temp["november"] = 0.0;
            $temp["december"] = 0.0;
            $temp["target_type"] = 2;
            $w = [];
            $w[] = ["year", "=", $temp['year']];
            $w[] = ["user_id", "=", $temp['user_id']];
            $w[] = ["target_type", "=", 2];
            $result = [];
            $result['where'] = $w;
            if ($this->dao->getCount($result) == 0) {
                $this->dao->add($temp);
            }
        }
        return _ok('操作成功');
    }


}
    